clear;
load('options_BE.mat')
load('Data_UK_revenue.mat', 'Data_UK_revenue')
nsect = length(Data_UK_revenue);
%nsect=1
kjInd_UK_rev = zeros(nsect,1);

for s = 1:nsect
    sigma=Data_UK_revenue(s,3);    
    %sigma=1.0
    k_cool = @(k_j)sd_log_rev_int(k_j, sigma);
    x = fsolve(k_cool,5, options_BE)
    kjInd_UK_rev(s,1)=x;
    save('kjInd_UK_rev.mat', 'kjInd_UK_rev');
end

save('kjInd_UK_rev.mat', 'kjInd_UK_rev');

kappa1 = zeros(nsect,1);
kappa2 = zeros(nsect,1);
theta = zeros(nsect,1);

for s = 1:nsect
fun3 = @(z, k) (1-(z.^2)).*(z.*(exp(z))).^k .*exp(z);
Integral3 = integral(@(z)fun3(z, kjInd_UK_rev(s,1)),0,1);

kappa1(s,1)=kjInd_UK_rev(s,1).*exp(-kjInd_UK_rev(s,1)-1).*Integral3;
kappa2(s,1)=kappa1(s,1)./kjInd_UK_rev(s,1);
theta(s,1)=(kappa2(s,1).*(kjInd_UK_rev(s,1)+1))./((1./(kjInd_UK_rev(s,1)+1))-(kappa1(s,1)+kappa2(s,1))  );
end
thetajInd_UK_rev=theta;
save('thetajInd_UK_rev.mat', 'thetajInd_UK_rev');


init = ones(nsect,1);
CC=Data_UK_revenue(:,4);
AA=theta;
ns=s;
beta_cool= @(B)eqsystem(AA, B, CC, ns);
    x = fsolve(beta_cool, init, options_BE)
    betajInd_UK_rev=x;
    save('betajInd_UK_rev.mat', 'betajInd_UK_rev');
    
